<?php
/**
 * @特惠赚 角色控制器
 * Author: 小谢
 * Date: 2019-5-8
 */
namespace app\admin\controller; 
use think\Controller;
use think\Db;
use think\facade\Session;
use think\facade\View;

class Group extends Base {

    //分组列表
    public function index(){
        $list = M('right_group')->order('orderby desc')->select();
        View::share(["list"=>$list]);
        return view();
    }
    
    //角色信息
    public function info(){
        $id = I('get.id/d');

        //控制器名称
        $planPath = APP_PATH.'admin/controller';
        $planList = array();
        $dirRes   = opendir($planPath);
        while($dir = readdir($dirRes))
        {
            if(!in_array($dir,array('.','..','.svn')))
            {
                $planList[] = basename($dir,'.php');
            }
        }

        if($id){
            $detail = M('right_group')->where("id",$id)->find();
            View::share(["detail"=>$detail]);
        }
        View::share(['planList'=>$planList]);
        return view();
    }
    
    //操作角色
    public function groupSave(){
        $data = I('post.data');
        if($data) {
            if(empty($data['name'])) {
                $this->error("分组名称不能为空!",U('Admin/group/index'));
            }
            $group = M('right_group')->where(['ctl'=>$data['ctl']])->column('id');
            if($group) {
                $this->error("分组已存在!",U('Admin/group/index'));
            }
            //编辑分组
            if($data['id']) {
                $res = M('right_group')->where(['id'=>$data['id']])->update($data);
            } else {
                //添加分组
                $res = M('right_group')->add($data);
            }
        }
  
        if($res){
            $this->success("操作成功!",U('Admin/group/index'));
        }
        $this->error("操作失败!",U('Admin/group/index'));

    }
    
    //删除角色
    public function del(){
        $id = I('post.id/d');
        $result = M('right_group')->where('id',$id)->delete();
        if($result){
            $this->ajaxReturn(1);
        }
        $this->ajaxReturn("删除失败");
    }
      
}